package com.hx.vending.controller;

import com.hx.vending.annotation.Accredit;
import com.hx.vending.bean.*;
import com.hx.vending.bean.entity.EquipmentDentity;
import com.hx.vending.bean.entity.EquipmentTypeVO;
import com.hx.vending.service.IEquipmentService;
import com.hx.vending.service.IEquipmentTypeService;
import com.hx.vending.util.DateUtil;
import com.hx.vending.util.Msg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * Created by Administrator on 2019/1/12.
 */
@Controller
@RequestMapping("/equipmentType")
public class EquipmentTypeController {

    @Autowired
    private IEquipmentTypeService equipmentTypeService;
    @Autowired
    private IEquipmentService equipmentService;

    /**
     * 查询所有设备类型
     * @return
     */
    @RequestMapping("/getEquipmentType")
    @ResponseBody
    @Accredit
    public Msg getEquipmentType(String typeName,@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum){
        pageNum = (pageNum - 1) * 10;
        return equipmentTypeService.getEquipmentType(typeName,pageNum);
    }

    /**
     * 查询设备货道
     * @return
     */
    @Accredit
    @RequestMapping("/getEquipmentTrails")
    @ResponseBody
    public Msg getEquipmentTrails(Integer equipmentCaseId){
        return equipmentTypeService.getEquipmentTrails(equipmentCaseId);
    }

    /**
     * 添加设备类型
     * @param equipmentType
     * @return
     */
    @Accredit
    @RequestMapping("/saveEquipmentType")
    @ResponseBody
    public Msg saveEquipmentType(@RequestBody EquipmentType equipmentType){
        equipmentType.setEquipmentTypeTime(DateUtil.getTime());
        int i = equipmentTypeService.insertEquipmentType(equipmentType);
        if(i > 0){
            return Msg.success();
        }
        return Msg.fail();
    }


    /**
     * 添加设备机箱，层
     * @param equipmentTypeVO
     * @return
     */
    @RequestMapping(value = "/saveEquipmentTypeRise",method = RequestMethod.POST)
    @ResponseBody
    @Accredit
    public Msg saveEquipmentTypeRise(@RequestBody EquipmentTypeVO equipmentTypeVO){
        return equipmentTypeService.insertTypeAndRise(equipmentTypeVO);
    }

    /**
     * 修改货道
     * @return
     */
    @ResponseBody
    @RequestMapping("/editEquipmentTypeTrails")
    @Accredit
    public Msg editEquipmentTypeTrails(@RequestBody EquipmentTypeTrails equipmentTypeTrails){
        return equipmentTypeService.editEquipmentTypeTrails(equipmentTypeTrails);
    }

    /**
     * 修改设备类型
     * @param equipmentType
     * @return
     */
    @RequestMapping("/deitEquipmentType")
    @ResponseBody
    @Accredit
    public Msg editEquipmentType(@RequestBody EquipmentType equipmentType){
        return equipmentTypeService.editEquipmentType(equipmentType);
    }

    /**
     * 修改设备机柜
     * @return
     */
    @RequestMapping("/editEquipmentTypeCase")
    @ResponseBody
    @Accredit
    public Msg editEquipmentTypeCase(@RequestBody EquipmentTypeVO equipmentTypeVO){

        return equipmentTypeService.editEquipmentTypeCase(equipmentTypeVO);
    }

    /**
     *
     * 修改层，货道(无用)
     * @param equipmentTypeRise
     * @return
     */
    @RequestMapping("/editEquipmentTypeRise")
    @ResponseBody
    public Msg editEquipmentTypeRise(@RequestBody EquipmentTypeRise equipmentTypeRise){
        return equipmentTypeService.editEquipmentTypeRise(equipmentTypeRise);
    }

    /**
     * 删除设备类型，机柜，层，货道
     * @param equipmentTypeIds
     * @return
     */
    @RequestMapping(value = "/deleteEquipmentTypeRise/{equipmentTypeIds}",method = RequestMethod.DELETE)
    @ResponseBody
    @Accredit
    public Msg deleteEquipmentTypeRise(@PathVariable String equipmentTypeIds){
        return equipmentTypeService.deleteEquipmentTypeRise(equipmentTypeIds);
    }

    /**
     * 删除设备机柜，层，货道
     * @param equipmentTypeCaseIds
     * @return
     */
    @RequestMapping(value = "/deleteEquipmentCase/{equipmentTypeCaseIds}",method = RequestMethod.DELETE)
    @ResponseBody
    @Accredit
    public Msg deleteEquipmentCase(@PathVariable String equipmentTypeCaseIds){

        return equipmentTypeService.deleteEquipmentCase(equipmentTypeCaseIds);
    }

    /**
     * 获取所有设备（后台）
     * @param equipmentId
     * @param equipmentType
     * @param userName
     * @param equipmentUseCondition 使用情况(0.可用  1.禁用)
     * @param equipmentState 状态(在线1   离线2)
     * @param pageNum
     * @return
     */
    @RequestMapping("/getEquipment")
    @ResponseBody
    @Accredit
    public Msg getEquipment(String equipmentId,String equipmentType,String userName,Integer equipmentUseCondition, Integer equipmentState,
                            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum){
        Integer num = equipmentService.selectEquipmentNum(equipmentId,equipmentType,userName,equipmentUseCondition,equipmentState);
        pageNum = (pageNum - 1) * 10;
        List<EquipmentDentity> equipmentList = equipmentService.selectByExamplePage(equipmentId,equipmentType,userName,equipmentUseCondition,equipmentState,pageNum);
        return Msg.success().add("equipmentList",equipmentList).add("num",num);
    }

    /**
     * 修改设备
     * @param equipment
     * @return
     */
    @RequestMapping("/editEquipment")
    @ResponseBody
    @Accredit
    public Msg editEquipment(@RequestBody Equipment equipment){
        int i = equipmentService.updateByPrimaryKeySelective(equipment);
        if(i > 0){
            return Msg.success();
        }else{
            return Msg.fail();
        }
    }

    /**
     * 根据设备类型修改设备货道信息
     * @param equipmentId
     * @param equipmentTypeId
     * @return
     */
    @RequestMapping("/updateEquipmentByType")
    @ResponseBody
    public Msg updateEquipmentByType(String equipmentId, Integer equipmentTypeId) {

        return this.equipmentTypeService.updateEquipmentByType(equipmentId, equipmentTypeId);
    }

    /**
     * 查询设备的货道数量(Android)
     * @param equipmentId
     * @return
     */
    @RequestMapping(value = "/selectEquipmentTypeByEquipment", method = RequestMethod.POST)
    @ResponseBody
    public Msg selectEquipmentTypeByEquipment(String equipmentId) {
        return this.equipmentTypeService.selectEquipmentTypeByEquipment(equipmentId);
    }

}
